package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IPrdContChargeService;
import com.wondertek.poms.dao.po.PrdContCharge;
import com.wondertek.poms.service.vo.PrdContChargeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "PrdContChargeController", description = "节目批价配置")
@RestController
@RequestMapping(value="/prdCont-charge")
public class PrdContChargeController {

    @Autowired
    private IPrdContChargeService prdContChargeService;

    @ApiOperation("节目批价配置 列表")
    @GetMapping(value="/list")
    @ResponseBody
    public ResultBean list(PrdContChargeVo prdContChargeVo,
                           @RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "20") int rows,
                           @RequestParam(defaultValue = "updateTime") String sidx,
                           @RequestParam(defaultValue = "desc") String sord){
        PrdContCharge prdContCharge=new PrdContCharge();
        BeanUtils.copyProperties(prdContChargeVo,prdContCharge);
        Page<PrdContCharge> list=prdContChargeService.findAll(prdContCharge,page,rows, sord,sidx);
        return PageBean.ok(list.getTotalPages(),list.getTotalElements(),list.getContent());
    }

    @ApiOperation("节目批价配置 添加")
    @PostMapping(value = "/add")
    @ResponseBody
    public ResultBean add(@RequestBody PrdContChargeVo prdContChargeVo){
        List<String> messageList = ValidateFilter.getFilterMessage(prdContChargeVo, Constants.VALIDATE_DEFAULT);
        if (!messageList.isEmpty()) {
            return ResultBean.error(messageList.toString());
        }
        if(isRepeat(prdContChargeVo)){
            return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
        }
        PrdContCharge prdContCharge=new PrdContCharge();
        BeanUtils.copyProperties(prdContChargeVo,prdContCharge);
        PrdContCharge.putTimeAndUser(prdContCharge);
        prdContChargeService.save(prdContCharge);
        return ResultBean.ok(prdContCharge);
    }




    public boolean isRepeat(PrdContChargeVo prdContChargeVo){
        PrdContCharge temp=new PrdContCharge();
        temp.setPrdContId(prdContChargeVo.getPrdContId());
        Optional<PrdContCharge> prdContChargeOpt=prdContChargeService.findOne(Example.of(temp));
        if(prdContChargeOpt.isPresent()){
            PrdContCharge prdContCharge=prdContChargeOpt.get();
            if (prdContChargeVo.getPrdContId() != null && prdContChargeVo.getPrdContId() != prdContCharge.getPrdContId())
                return true;
            else if (prdContChargeVo.getPrdContId() == null)
                return true;
        }
        return false;
    }
}
